package com.cat.dataStructure;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/shift-distance-between-two-strings/description/
 * @create 2025/8/16 09:22
 * @since JDK17
 */

public class Solution23 {
    static long[] pre1 = new long[27], pre2 = new long[27];

    public long shiftDistance(String s, String t, int[] nextCost, int[] previousCost) {
        int n = s.length();
        long ans = 0;

        for (int i = 0; i < 26; i++) {
            pre1[i + 1] = pre1[i] + nextCost[i];
            pre2[i + 1] = pre2[i] + previousCost[i];
        }
        char a, b, c;
        for (int i = 0; i < n; i++) {
            a = s.charAt(i);
            b = t.charAt(i);
            if (a > b) {    // 正着换
                c = a;
                a = b;
                b = c;
            }
            ans += Math.min(pre1[b - 'a'] - pre1[a - 'a'], pre2[26] - pre2[b]);
        }

        return ans;
    }
}
